package com.water.app.Controller.comment;

import com.water.app.Constant;
import com.water.app.Result;
import com.water.app.comment;
import com.water.app.interfaces.comment.CommentService;
import com.water.app.mapper.commentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CommentController {
    private static final Logger log= LoggerFactory.getLogger(CommentController.class);
    @Autowired
    private CommentService commentService;
    @PostMapping("/userComment")
    public Result comment(@RequestBody comment comments){
        String comment = comments.getComment();
        int goodsId = comments.getGoodsId();
        int userId=Constant.THREAD_LOCAL.get();
        log.info("用户{}上传评论{}",userId,comment);
        if(comment.isEmpty()){
            log.info("用户没有上传任何评论");
            return Result.error("上传评论失败");
        }
        boolean result= commentService.postComment(userId,goodsId,comment);
        if(!result){
            log.info("用户{}上传评论失败",userId);
            return Result.error("上传评论失败");
        }
        log.info("用户{}上传评论成功",userId);
        return Result.success("上传评论成功");
    }

    @GetMapping("selectComment/{goodsId}")
    public Result userComment(@PathVariable int goodsId){
        log.info("正在查询{}商品的评论",goodsId);
        if(goodsId<=0){
            log.info("传参错误");
            return Result.error("传参错误");
        }
        List<comment> list= commentService.selectComment(goodsId);
        if (list.isEmpty()){
            log.info("暂无评论");
            return Result.error("暂无评论");
        }
        log.info("查询评论成功");
        return Result.success(1,"查询评论成功",list);
    }
}
